package com.lagou.controller;

import com.lagou.dao.StudentDaoImpl;
import com.lagou.entity.Clazz;
import com.lagou.entity.Student;
import com.lagou.service.StudentService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet(name = "AddServlet",urlPatterns = "/add")
public class AddServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        StudentService studentService = new StudentService(new StudentDaoImpl());
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String sex = request.getParameter("sex");
        String birthday = request.getParameter("birthday");
        String age = request.getParameter("age");
        String cname = request.getParameter("cname");
        String cid = request.getParameter("cid");
        Clazz clazz = new Clazz(Integer.valueOf(cid),cname);
        Student student = new Student(username,password,sex,birthday,Integer.valueOf(age),Integer.valueOf(cid),cname);
        student.setClazz(clazz);
        System.out.println(student);
        int i = studentService.add(student);
        if(i==1){
            System.out.println("添加成功!");
        }else{
            System.out.println("添加失败!");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
